package com.txunda.zhuohired.adapter.user.home;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.home.JobDetailsAty;
import com.txunda.zhuohired.bean.user.home.CompanyOneBean;

import java.util.List;


/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/27 9:18
 * 功能描述：公司详情-全部职位adapter
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyJobListAdapter extends BaseAdapter {

    private Context context;
    private ViewHolder holder;
    private List<CompanyOneBean.DataBean.InfoDataBean> companyInfoList;

    public CompanyJobListAdapter(Context context, List<CompanyOneBean.DataBean.InfoDataBean> companyInfoList) {
        this.context = context;
        this.companyInfoList = companyInfoList;
    }

    @Override
    public int getCount() {
        return companyInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return companyInfoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_company_job_list, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (TextUtils.isEmpty(companyInfoList.get(i).getTitle())) {
            holder.tv_item_company_job_list_tittle.setVisibility(View.GONE);
        } else {
            holder.tv_item_company_job_list_tittle.setVisibility(View.VISIBLE);
            holder.tv_item_company_job_list_tittle.setText(companyInfoList.get(i).getTitle());
        }
        if (TextUtils.isEmpty(companyInfoList.get(i).getAddress()) && TextUtils.isEmpty(companyInfoList.get(i)
                .getClaim_experience()) && TextUtils.isEmpty(companyInfoList.get(i).getClaim_shooling())) {
            holder.tv_item_company_job_list_address.setVisibility(View.GONE);
        } else {
            holder.tv_item_company_job_list_address.setVisibility(View.VISIBLE);
            holder.tv_item_company_job_list_address.setText(companyInfoList.get(i).getAddress() + "-" +
                    companyInfoList.get(i).getClaim_shooling() + "-" + companyInfoList.get(i)
                    .getClaim_experience());
        }
        if (TextUtils.isEmpty(companyInfoList.get(i).getName())) {
            holder.tv_company_job_list_name.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_company_job_list_name.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_name.setText(companyInfoList.get(i).getName());
        }
        if (TextUtils.isEmpty(companyInfoList.get(i).getPrice())) {
            holder.tv_item_company_job_list_price.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_company_job_list_price.setVisibility(View.VISIBLE);
            holder.tv_item_company_job_list_price.setText(companyInfoList.get(i).getPrice());
        }
        String string = companyInfoList.get(i).getWelfare();
        String[] stringArr = string.split(",");
        if (TextUtils.isEmpty(companyInfoList.get(i).getWelfare())) {
            holder.ll_company_job_list_welfare.setVisibility(View.GONE);
        } else if (stringArr.length == 1) {
            holder.ll_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setText(stringArr[0]);
            holder.tv_company_job_list_welfare_one.setVisibility(View.GONE);
            holder.tv_company_job_list_welfare_two.setVisibility(View.GONE);
        } else if (stringArr.length == 2) {
            holder.ll_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setText(stringArr[0]);
            holder.tv_company_job_list_welfare_one.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare_one.setText(stringArr[1]);
            holder.tv_company_job_list_welfare_two.setVisibility(View.GONE);
        } else if (stringArr.length == 3) {
            holder.ll_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare.setText(stringArr[0]);
            holder.tv_company_job_list_welfare_one.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare_one.setText(stringArr[1]);
            holder.tv_company_job_list_welfare_two.setVisibility(View.VISIBLE);
            holder.tv_company_job_list_welfare_two.setText(stringArr[2]);
        }
        if (TextUtils.isEmpty(companyInfoList.get(i).getPosition_good())) {
            holder.tv_item_company_job_list_details.setVisibility(View.GONE);
        } else {
            holder.tv_item_company_job_list_details.setVisibility(View.VISIBLE);
            holder.tv_item_company_job_list_details.setText(companyInfoList.get(i).getPosition_good());
        }
        holder.ll_company_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,JobDetailsAty.class);
                intent.putExtra("job_detail_id",companyInfoList.get(i).getId());
                context.startActivity(intent);
            }
        });
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.ll_company_one)
        private LinearLayout ll_company_one;
        /**
         * 职位名称
         */
        @ViewInject(R.id.tv_item_company_job_list_tittle)
        private TextView tv_item_company_job_list_tittle;
        /**
         * 公司地址
         */
        @ViewInject(R.id.tv_item_company_job_list_address)
        private TextView tv_item_company_job_list_address;
        /**
         * 公司名称
         */
        @ViewInject(R.id.tv_company_job_list_name)
        private TextView tv_company_job_list_name;
        /**
         * 薪资
         */
        @ViewInject(R.id.tv_item_company_job_list_price)
        private TextView tv_item_company_job_list_price;
        /**
         * 福利
         */
        @ViewInject(R.id.ll_company_job_list_welfare)
        private LinearLayout ll_company_job_list_welfare;
        @ViewInject(R.id.tv_company_job_list_welfare)
        private TextView tv_company_job_list_welfare;
        @ViewInject(R.id.tv_company_job_list_welfare_one)
        private TextView tv_company_job_list_welfare_one;
        @ViewInject(R.id.tv_company_job_list_welfare_two)
        private TextView tv_company_job_list_welfare_two;
        /**
         * 公司详情
         */
        @ViewInject(R.id.tv_item_company_job_list_details)
        private TextView tv_item_company_job_list_details;
    }
}
